//
//  WHomeTwoView.swift
//  iiatn
//
//  Created by mine on 18/4/10.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD

protocol HomeTwoViewDelegate: NSObjectProtocol {
    func homeTwo(button: UIButton)
}

class WHomeTwoView: UIViewController {

    @IBOutlet var tview: UIView!
    
    @IBOutlet var xzbtn: UIButton!
    
    @IBOutlet var fbbtn: UIButton!
    
    @IBOutlet var btn3: UIButton!
    
    @IBOutlet var btn4: UIButton!
    
    @IBOutlet var btn5: UIButton!
    
    @IBOutlet var btn6: UIButton!
    
    @IBOutlet var btn7: UIButton!
    
    @IBOutlet var btn8: UIButton!
    
    @IBOutlet var btn9: UIButton!
    
    @IBOutlet var btn10: UIButton!
    
    @IBOutlet var btn11: UIButton!
    
    @IBOutlet var btn12: UIButton!
    
    @IBOutlet var btn13: UIButton!
    
    @IBOutlet var btn14: UIButton!
    
    @IBOutlet var btn15: UIButton!
    
    @IBOutlet var btn16: UIButton!
    
    @IBOutlet weak var xian1: UIProgressView!
    
    @IBOutlet weak var xian2: UIProgressView!
    
    @IBOutlet weak var xian3: UIProgressView!
    
    @IBOutlet weak var xian4: UIProgressView!
    
    @IBOutlet weak var kuar1: UIButton!
    
    @IBOutlet weak var kuar2: UIButton!
    
    
    var btns = [UIButton]()
    
    var fwbtns = [UIButton]()
    
    var xians = [UIProgressView]()
    
    weak var delegate: HomeTwoViewDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setbtns()
        setStyle()
        setplace()
        setdosth()
        setweitiao()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setbtns(){
        btns.append(btn3)
        btns.append(btn4)
        btns.append(btn5)
        btns.append(btn6)
        btns.append(btn7)
        btns.append(btn8)
        btns.append(btn9)
        btns.append(btn10)
        
        fwbtns.append(btn11)
        fwbtns.append(btn12)
        fwbtns.append(btn13)
        fwbtns.append(btn14)
        fwbtns.append(btn15)
        fwbtns.append(btn16)
        
        xians.append(xian1)
        xians.append(xian2)
        xians.append(xian3)
        xians.append(xian4)
    }
    func setdosth(){
        xzbtn.tag = 1
        fbbtn.tag = 2
        xzbtn.addTarget(self, action: #selector(groupButonClick), for: .touchUpInside)
        fbbtn.addTarget(self, action: #selector(groupButonClick), for: .touchUpInside)
        
        var index = 3
        let imgname = "_60x60_"
        while index < 11
        {
            btns[index-3].tag = index
            btns[index-3].addTarget(self, action: #selector(groupButonClick), for: .touchUpInside)
            // Mark: setTouchDown
            btns[index-3].setImage(UIImage(named: String(index-2) + imgname + "selected"), for: .highlighted)
            
            index = index + 1
        }
        
        var fwindex = 11
        while fwindex < 17
        {
            fwbtns[fwindex-11].tag = fwindex
            fwbtns[fwindex-11].addTarget(self, action: #selector(groupButonClick), for: .touchUpInside)
            fwindex = fwindex + 1
        }
    }
    
    func setplace(){
        for btn in btns{
            btn.set(image: btn.currentImage!, title: btn.currentTitle!, titlePosition: .bottom,
                     additionalSpacing: 10.0, state: .normal)
        }
        
        var ii = 0
        for fwbtn in fwbtns{
            fwbtn.layer.borderWidth = 0.7
            fwbtn.layer.borderColor = WlightGrayColor().cgColor
            fwbtn.width = SCREENW/2
            if ii%2 == 0 {
                fwbtn.x = 0
            }else{
                fwbtn.x = SCREENW/2 - 0.4
            }
            fwbtn.set(image: fwbtn.currentImage!, title: fwbtn.currentTitle!, titlePosition: .left,
                    additionalSpacing: 25.0, state: .normal)
            ii = ii + 1

        }
    }
    
    func setStyle(){
        let xx = SCREENW - 30
        
        tview.width = SCREENW
        
        xzbtn.x = 8
        xzbtn.width = xx/2
        fbbtn.width = xx/2
        fbbtn.x = SCREENW - xzbtn.width - 15
        
        xzbtn.layer.cornerRadius = xzbtn.height * 0.1
        xzbtn.layer.masksToBounds = true
        xzbtn.layer.rasterizationScale = UIScreen.main.scale
        
        fbbtn.layer.cornerRadius = fbbtn.height * 0.1
        fbbtn.layer.masksToBounds = true
        fbbtn.layer.rasterizationScale = UIScreen.main.scale
        
        let mbtnwidth = btn3.width
        let mblack = (SCREENW - mbtnwidth*4)/5
        
        var bindex = 0
        while bindex < 8
        {
            btns[bindex].x = mblack*CGFloat(bindex%4+1)+mbtnwidth*CGFloat(bindex%4)
            bindex = bindex + 1
        }
        
    }
    
    func groupButonClick(button: UIButton) {
        delegate?.homeTwo(button: button)
    }
    
    func setweitiao(){
        kuar1.layer.cornerRadius = kuar1.height * 0.05
        kuar1.layer.masksToBounds = true
        kuar1.layer.rasterizationScale = UIScreen.main.scale
        kuar2.layer.cornerRadius = kuar2.height * 0.05
        kuar2.layer.masksToBounds = true
        kuar2.layer.rasterizationScale = UIScreen.main.scale
        
        for xian in xians {
            xian.width = SCREENW
            xian.height = 0.5
            xian.setProgress(0.0, animated: false)
            xian.trackTintColor = WlightGrayColor()
        }
    }

}
